import $ from 'jquery';
global.$ = global.jQuery = $;
import { showMsg } from "./../../js/base/utils";
require("./../../js/base/captcha");
import './../../js/layui/css/layui.css';
require("./../../js/layui/layui");
var layer = layui.layer;

(function ($) {
    var is_smssend = false, smssend_interval = null;     //标记短信发送中
    $("#recaptcha_sms").captcha();

    $("#smssend").click(function () {
        if (is_smssend) {     //短信发送中60s后发送
            return false;
        }
        var sms_mobile = $("#smsmobile").val();
        var changeMobileFirst = $("#imgType").val();
        var imgCode = $("#imgCode").val();
        if (!imgCode) {
            showMsg(400, "图形验证码不能为空!");
            return false;
        }

        if (!sms_mobile) {
            showMsg(400, "手机号码不能为空!");
            return false;
        }

        var data = { mobile: sms_mobile, type: changeMobileFirst, imgCode: imgCode };
        dataStr = JSON.stringify(data);
        $.postJSON(
            app_glob_sendCaptcha,
            dataStr,
            function (responseText) {
                if (typeof responseText == "string") {
                    var responseText = $.parseJSON(responseText);
                }
                if (responseText.code == '200') {
                    layer.closeAll();
                    smssendInterval();   // 开启倒计时
                } else {
                    showMsg(responseText.code, responseText.message);
                    //图片模拟点击
                    $("#recaptcha_sms").trigger("click");
                }
            }
        );
    });

    // 开启短信发送中状态定时器
    function smssendInterval() {
        is_smssend = true;      //开启倒计时状态
        clearInterval(smssend_interval);

        var time = 60;
        $('#smssend').text('请' + time + 's后重新获取')

        smssend_interval = setInterval(function () {
            if (time <= 1) {
                clearInterval(smssend_interval);
                is_smssend = false;
                $('#smssend').text('点击发送验证码')
                return false;
            }
            time--;
            $('#smssend').text('请' + time + 's后重新获取')
        }, 1000);

    }
})(jQuery);
